//package com.ehualu.getes.until;
//
//import org.apache.http.HttpHost;
//import org.elasticsearch.action.search.SearchRequest;
//import org.elasticsearch.action.search.SearchResponse;
//import org.elasticsearch.client.RestClient;
//import org.elasticsearch.client.RestClientBuilder;
//import org.elasticsearch.client.RestHighLevelClient;
//import org.elasticsearch.index.query.QueryBuilder;
//import org.elasticsearch.index.query.QueryBuilders;
//import org.elasticsearch.search.SearchHit;
//import org.elasticsearch.search.SearchHits;
//import org.elasticsearch.search.builder.SearchSourceBuilder;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//import java.io.IOException;
//import java.text.SimpleDateFormat;
//import java.util.*;
//
//public class EsUtiltes {
//    private static final Logger LOGGER = LoggerFactory.getLogger(EsUtiltes.class);
//
//
//    /**
//     * 获取client连接
//     */
//    public static RestHighLevelClient getHighLevelClient(String host, int port, String schema) {
//       /* //单个ip创建方式
//        HttpHost  httpHost= new HttpHost(host, port, schema);
//        RestClientBuilder builder = RestClient.builder(httpHost);
//        RestHighLevelClient  client = new RestHighLevelClient(builder);
//        return client;*/
//        String[] ipArr = host.split(",");
//        HttpHost[] httpHosts = new HttpHost[ipArr.length];
//        for (int i = 0; i < ipArr.length; i++) {
//            httpHosts[i] = new HttpHost(ipArr[i], port, schema);
//        }
//        RestClientBuilder builder = RestClient.builder(httpHosts);
//        RestHighLevelClient client = new RestHighLevelClient(builder);
//        return client;
//    }
//
//    public static RestClient getLowLevelClient(RestHighLevelClient client) {
//        return client.getLowLevelClient();
//    }
//
//
//    public static void getshij(String starttime, String endTimeStr, String index, String kkid, RestHighLevelClient client) {
//        try {
//
//            String indexName = index;
//
//            SearchRequest searchRequest = new SearchRequest(indexName);
//            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
////			sourceBuilder.from(0); // 默认0
//            sourceBuilder.size(10000);// 默认10条
//
//            QueryBuilder queryBuilder = QueryBuilders.boolQuery()
//                    .must(QueryBuilders.termQuery("caddresscode", kkid))
//                    .must(QueryBuilders.rangeQuery("createtime").from(starttime).to(endTimeStr));
//            sourceBuilder.query(queryBuilder);
//            searchRequest.source(sourceBuilder);
//            SearchResponse response = client.search(searchRequest);
//            SearchHits hits = response.getHits();
//            SearchHit[] searchHits = hits.getHits();
//            int sl = searchHits.length;
//            System.out.println("查询数据量：" + sl);
//            for (SearchHit hit : searchHits) {
//
//
//                Map<String, Object> sourceAsMap = hit.getSourceAsMap();// 获取查询结果json
//
//                String strccarnumber = (String) sourceAsMap.get("ccarnumber");
//                String strid = (String) sourceAsMap.get("dtid");
//
//                System.out.println("strccarnumber:" + strccarnumber);
//                System.out.println("strid:" + strid);
////				log.info("索引：" + index + ",类型：" + type + ",ID：" + id + ",得分：" + score + "," + sourceAsMap);
////				return sourceAsMap.get("pic1") + "";
//            }
//            System.out.println("循环*****结束");
//
////			return pic;
//        } catch (IOException e) {
//            e.printStackTrace();
//        } catch (Exception e1) {
//            e1.printStackTrace();
//        }
//    }
//
//
//    public static int getshij2(String starttime, String endTimeStr, String index, String kkid, RestHighLevelClient client) {
//        try {
//
//            String indexName = index;
//
//            SearchRequest searchRequest = new SearchRequest(indexName);
//            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
////			sourceBuilder.from(0); // 默认0
//            sourceBuilder.size(10000);// 默认10条
//
//            QueryBuilder queryBuilder = QueryBuilders.boolQuery()
//                    .must(QueryBuilders.termQuery("caddresscode", kkid))
//                    .must(QueryBuilders.rangeQuery("createtime").from(starttime).to(endTimeStr));
//            sourceBuilder.query(queryBuilder);
//            searchRequest.source(sourceBuilder);
//            SearchResponse response = client.search(searchRequest);
//            SearchHits hits = response.getHits();
//            SearchHit[] searchHits = hits.getHits();
//            int sl = searchHits.length;
//            System.out.println("查询数据量：" + sl);
//            for (SearchHit hit : searchHits) {
//
//
//                Map<String, Object> sourceAsMap = hit.getSourceAsMap();// 获取查询结果json
//
//                String strccarnumber = (String) sourceAsMap.get("ccarnumber");
//                String strid = (String) sourceAsMap.get("dtid");
//
//                System.out.println("strccarnumber:" + strccarnumber);
//                System.out.println("strid:" + strid);
////				log.info("索引：" + index + ",类型：" + type + ",ID：" + id + ",得分：" + score + "," + sourceAsMap);
////				return sourceAsMap.get("pic1") + "";
//            }
//            System.out.println("循环*****结束");
//            return sl;
//
////			return pic;
//        } catch (IOException e) {
//            e.printStackTrace();
//        } catch (Exception e1) {
//            e1.printStackTrace();
//        }
//        return 0;
//    }
//
//    /**
//     * 国道:G105 道路代码
//     * 48288
//     * 48198
//     * 48181
//     * 48229
//     * 48072
//     * 48088
//     * 10105
//     * 48168
//     * 48266
//     * 48281
//     * 48282
//     * 48151
//     * 48153
//     * 48300
//     * 46688
//     * 46077
//     * 67102
//     * 46545
//     * 48157
//     * 48164
//     */
//    public static Map<String, String> getGliuluntil(String index, String nowtime, String dlm, String fx, RestHighLevelClient client) {
//
//        String res;
////        设置时间格式
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
////        设置时区
//        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
//
////时间戳转成date类型
//        long lt = new Long(nowtime);
//        Date date = new Date(lt);
//        res = simpleDateFormat.format(date);
//        System.out.println("********当前时间转换时区后*********" + res);
//
//        Calendar cal = Calendar.getInstance();
//        cal.setTime(date);
//
////        时间向前推七天
//        cal.add(Calendar.DATE, -7);
//
//        Date dabef = cal.getTime();
//
//        String outtime = simpleDateFormat.format(dabef);
//
//        System.out.println("********当前时间转换时区并往前推7天后*********" + outtime);
//
//
//        String indexName = index;
//
//        SearchRequest searchRequest = new SearchRequest(indexName);
//        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
////			sourceBuilder.from(0); // 默认0
////        sourceBuilder.size(10000);// 默认10条
//
//
//        Map<String, Integer> dlkknum = new HashMap<>();
//
//
//
//
//        QueryBuilder queryBuilderG105 = QueryBuilders.boolQuery()
//                .must(QueryBuilders.termsQuery("dldm", 48288, 48198, 48181, 48229, 48072, 48088, 10105, 48168, 48266, 48281, 48282, 48151, 48153, 48300, 46688, 46077, 67102, 46545, 48157, 48164))
//                .must(QueryBuilders.termQuery("xsfx", fx))
//                .must(QueryBuilders.rangeQuery("createtime").from(outtime).to(res));
//
//        QueryBuilder queryBuilderG20 = QueryBuilders.boolQuery()
//                .must(QueryBuilders.termQuery("dldm", 00020))
//                .must(QueryBuilders.termQuery("xsfx", fx))
//                .must(QueryBuilders.rangeQuery("createtime").from(outtime).to(res));
//
//
//        sourceBuilder.query(queryBuilderG105);
//        searchRequest.source(sourceBuilder);
//        SearchResponse response = null;
//        try {
//            response = client.search(searchRequest);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        SearchHits hits = response.getHits();
//        Long rtlh = response.getHits().getTotalHits();
//        System.out.println(rtlh);
//
////        SearchHit[] searchHits = hits.getHits();
////        int sl = searchHits.length;
////        System.out.println("查询数据量：" + sl);
////        for (SearchHit hit : searchHits) {
////
////
////            Map<String, Object> sourceAsMap = hit.getSourceAsMap();// 获取查询结果json
////
////            String strccarnumber = (String) sourceAsMap.get("ccarnumber");
////            String strid = (String) sourceAsMap.get("dtid");
////
////            System.out.println("strccarnumber:" + strccarnumber);
////            System.out.println("strid:" + strid);
//////				log.info("索引：" + index + ",类型：" + type + ",ID：" + id + ",得分：" + score + "," + sourceAsMap);
//////				return sourceAsMap.get("pic1") + "";
////        }
//        System.out.println("循环*****结束");
//
//        return null;
//    }
//
//
//    /**
//     * 关闭连接
//     */
//    public static void close(RestHighLevelClient client) {
//        if (client != null) {
//            try {
//                client.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//    }
//}
